package cn.wq3stone.util;

import java.sql.*;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @author wq3stone
 */
public class DBUtilTest {

    private static ResourceBundle resourceBundle = ResourceBundle.getBundle("resources/db");
    private static Connection conn = null;
    private static String url = resourceBundle.getString("url");
    private static String username = resourceBundle.getString("username");
    private static String password = resourceBundle.getString("password");

    // 私有构造方法，防止实例化
    private DBUtilTest() {}

    // 静态代码块，加载驱动
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(String url,String username,String password){
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn, Statement stmt, ResultSet rs){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}